Skip to content

Commit

Permalink
Core Data: Fixes necessary for converting to TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
dmsnell committed Mar 4, 2022
1 parent 24ece16 commit 93d8751
Show file tree
Hide file tree
Showing 48 changed files with 234 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export default function createBatch( processor = defaultProcessor ) {
*/
async run() {
if ( pending.size ) {
await new Promise( ( resolve ) => {
await new Promise< void >( ( resolve ) => {
const unsubscribe = pending.subscribe( () => {
if ( ! pending.size ) {
unsubscribe();
Expand Down Expand Up @@ -160,7 +160,10 @@ export default function createBatch( processor = defaultProcessor ) {
};
}

class ObservableSet {
class ObservableSet< T > {
private set: Set< T >;
private subscribers: Set< () => void >;

constructor( ...args ) {
this.set = new Set( ...args );
this.subscribers = new Set();
Expand Down
2 changes: 1 addition & 1 deletion packages/core-data/src/batch/default-processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default async function defaultProcessor( requests ) {

const results = [];

for ( const batchRequests of chunk( requests, maxItems ) ) {
for ( const batchRequests of chunk( requests, maxItems ?? undefined ) ) {
const batchResponse = await apiFetch( {
path: '/batch/v1',
method: 'POST',
Expand Down
4 changes: 2 additions & 2 deletions packages/core-data/src/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ export const getMethodName = (
const nameSuffix =
upperFirst( camelCase( name ) ) + ( usePlural ? 's' : '' );
const suffix =
usePlural && entity.plural
usePlural && entity?.plural
? upperFirst( camelCase( entity.plural ) )
: nameSuffix;
return `${ prefix }${ kindPrefix }${ suffix }`;
Expand All @@ -286,7 +286,7 @@ export const getMethodName = (
*
* @param {string} kind Kind
*
* @return {Array} Entities
* @return {( thunkArgs: any ) => Promise<any[]>} Entities
*/
export const getKindEntities = ( kind ) => async ( { select, dispatch } ) => {
let entities = select.getEntitiesByKind( kind );
Expand Down
10 changes: 8 additions & 2 deletions packages/core-data/src/entity-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ const entities = {
if ( ! acc[ entity.kind ] ) {
acc[ entity.kind ] = {};
}
acc[ entity.kind ][ entity.name ] = { context: createContext() };
acc[ entity.kind ][ entity.name ] = {
context: createContext( undefined ),
};
return acc;
}, {} ),
...kinds.reduce( ( acc, kind ) => {
Expand All @@ -41,7 +43,7 @@ const getEntity = ( kind, type ) => {
}

if ( ! entities[ kind ][ type ] ) {
entities[ kind ][ type ] = { context: createContext() };
entities[ kind ][ type ] = { context: createContext( undefined ) };
}

return entities[ kind ][ type ];
Expand Down Expand Up @@ -126,6 +128,10 @@ export function useEntityProp( kind, type, prop, _id ) {
return [ value, setValue, fullValue ];
}

/** @typedef {Object} WPBlock A loaded block in memory
* @property {string} name Block name
*/

/**
* Hook that returns block content getters and setters for
* the nearest provided entity of the specified type.
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Internal dependencies
*/
import {
import type {
Context,
ContextualField,
MediaType,
Expand All @@ -12,7 +12,7 @@ import {
PingStatus,
} from './helpers';

import { BaseEntityTypes as _BaseEntityTypes } from './base-entity-types';
import type { BaseEntityTypes as _BaseEntityTypes } from './base-entity-types';

declare module './base-entity-types' {
export namespace BaseEntityTypes {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/**
* Internal dependencies
*/
import {
import type {
AvatarUrls,
Context,
ContextualField,
OmitNevers,
RenderedText,
} from './helpers';
import { BaseEntityTypes as _BaseEntityTypes } from './base-entity-types';
import type { BaseEntityTypes as _BaseEntityTypes } from './base-entity-types';

export type CommentStatus = 'hold' | 'approve' | 'spam' | 'trash' | '1' | '0';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Internal dependencies
*/
import { EntityRecord } from './index';
import type { EntityRecord } from './index';

export interface AvatarUrls {
/**
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* Internal dependencies
*/
import { Context, OmitNevers } from './helpers';
import type { Context, OmitNevers } from './helpers';

import { BaseEntityTypes as _BaseEntityTypes } from './base-entity-types';
import type { BaseEntityTypes as _BaseEntityTypes } from './base-entity-types';

declare module './base-entity-types' {
export namespace BaseEntityTypes {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
/**
* Internal dependencies
*/
import { RenderedText, Context, ContextualField, OmitNevers } from './helpers';
import type {
RenderedText,
Context,
ContextualField,
OmitNevers,
} from './helpers';

import { BaseEntityTypes as _BaseEntityTypes } from './base-entity-types';
import type { BaseEntityTypes as _BaseEntityTypes } from './base-entity-types';

export type NavMenuItemType =
| 'taxonomy'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* Internal dependencies
*/
import { Context, ContextualField, OmitNevers } from './helpers';
import type { Context, ContextualField, OmitNevers } from './helpers';

import { BaseEntityTypes as _BaseEntityTypes } from './base-entity-types';
import type { BaseEntityTypes as _BaseEntityTypes } from './base-entity-types';

declare module './base-entity-types' {
export namespace BaseEntityTypes {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* Internal dependencies
*/
import { Context, OmitNevers } from './helpers';
import type { Context, OmitNevers } from './helpers';

import { BaseEntityTypes as _BaseEntityTypes } from './base-entity-types';
import type { BaseEntityTypes as _BaseEntityTypes } from './base-entity-types';

declare module './base-entity-types' {
export namespace BaseEntityTypes {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Internal dependencies
*/
import {
import type {
CommentingStatus,
Context,
ContextualField,
Expand All @@ -11,7 +11,7 @@ import {
OmitNevers,
} from './helpers';

import { BaseEntityTypes as _BaseEntityTypes } from './base-entity-types';
import type { BaseEntityTypes as _BaseEntityTypes } from './base-entity-types';

declare module './base-entity-types' {
export namespace BaseEntityTypes {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
/**
* Internal dependencies
*/
import { Context, ContextualField, RenderedText, OmitNevers } from './helpers';
import type {
Context,
ContextualField,
RenderedText,
OmitNevers,
} from './helpers';

import { BaseEntityTypes as _BaseEntityTypes } from './base-entity-types';
import type { BaseEntityTypes as _BaseEntityTypes } from './base-entity-types';

declare module './base-entity-types' {
export namespace BaseEntityTypes {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Internal dependencies
*/
import {
import type {
CommentingStatus,
Context,
ContextualField,
Expand All @@ -12,7 +12,7 @@ import {
OmitNevers,
} from './helpers';

import { BaseEntityTypes as _BaseEntityTypes } from './base-entity-types';
import type { BaseEntityTypes as _BaseEntityTypes } from './base-entity-types';

declare module './base-entity-types' {
export namespace BaseEntityTypes {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
/**
* Internal dependencies
*/
import { CommentingStatus, Context, OmitNevers, PingStatus } from './helpers';
import type {
CommentingStatus,
Context,
OmitNevers,
PingStatus,
} from './helpers';

import { BaseEntityTypes as _BaseEntityTypes } from './base-entity-types';
import type { BaseEntityTypes as _BaseEntityTypes } from './base-entity-types';

declare module './base-entity-types' {
export namespace BaseEntityTypes {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/**
* Internal dependencies
*/
import { Widget } from './widget';
import { Context, OmitNevers } from './helpers';
import type { Context, OmitNevers } from './helpers';

import { BaseEntityTypes as _BaseEntityTypes } from './base-entity-types';
import type { BaseEntityTypes as _BaseEntityTypes } from './base-entity-types';

declare module './base-entity-types' {
export namespace BaseEntityTypes {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* Internal dependencies
*/
import { Context, ContextualField, OmitNevers } from './helpers';
import type { Context, ContextualField, OmitNevers } from './helpers';

import { BaseEntityTypes as _BaseEntityTypes } from './base-entity-types';
import type { BaseEntityTypes as _BaseEntityTypes } from './base-entity-types';

declare module './base-entity-types' {
export namespace BaseEntityTypes {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* Internal dependencies
*/
import { Context, PostFormat, RenderedText, OmitNevers } from './helpers';
import type { Context, PostFormat, RenderedText, OmitNevers } from './helpers';

import { BaseEntityTypes as _BaseEntityTypes } from './base-entity-types';
import type { BaseEntityTypes as _BaseEntityTypes } from './base-entity-types';

declare module './base-entity-types' {
export namespace BaseEntityTypes {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* Internal dependencies
*/
import { Context, ContextualField, OmitNevers } from './helpers';
import type { Context, ContextualField, OmitNevers } from './helpers';

import { BaseEntityTypes as _BaseEntityTypes } from './base-entity-types';
import type { BaseEntityTypes as _BaseEntityTypes } from './base-entity-types';

declare module './base-entity-types' {
export namespace BaseEntityTypes {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
/**
* Internal dependencies
*/
import { AvatarUrls, Context, ContextualField, OmitNevers } from './helpers';
import type {
AvatarUrls,
Context,
ContextualField,
OmitNevers,
} from './helpers';

import { BaseEntityTypes as _BaseEntityTypes } from './base-entity-types';
import type { BaseEntityTypes as _BaseEntityTypes } from './base-entity-types';

declare module './base-entity-types' {
export namespace BaseEntityTypes {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* Internal dependencies
*/
import { Context, OmitNevers } from './helpers';
import type { Context, OmitNevers } from './helpers';

import { BaseEntityTypes as _BaseEntityTypes } from './base-entity-types';
import type { BaseEntityTypes as _BaseEntityTypes } from './base-entity-types';

declare module './base-entity-types' {
export namespace BaseEntityTypes {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* Internal dependencies
*/
import { Context, ContextualField, OmitNevers } from './helpers';
import type { Context, ContextualField, OmitNevers } from './helpers';

import { BaseEntityTypes as _BaseEntityTypes } from './base-entity-types';
import type { BaseEntityTypes as _BaseEntityTypes } from './base-entity-types';

declare module './base-entity-types' {
export namespace BaseEntityTypes {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/**
* Internal dependencies
*/
import {
import type {
Context,
PostStatus,
RenderedText,
OmitNevers,
ContextualField,
} from './helpers';

import { BaseEntityTypes as _BaseEntityTypes } from './base-entity-types';
import type { BaseEntityTypes as _BaseEntityTypes } from './base-entity-types';

declare module './base-entity-types' {
export namespace BaseEntityTypes {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/**
* Internal dependencies
*/
import {
import type {
Context,
PostStatus,
RenderedText,
OmitNevers,
ContextualField,
} from './helpers';

import { BaseEntityTypes as _BaseEntityTypes } from './base-entity-types';
import type { BaseEntityTypes as _BaseEntityTypes } from './base-entity-types';

declare module './base-entity-types' {
export namespace BaseEntityTypes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ const fetchLinkSuggestions = async (
.slice( 0, perPage )
.map(
/**
* @param {{ id: number, url:string, title?:string, subtype?: string, type?: string }} result
* @param {{ id: number, meta?: object, url:string, title?:string, subtype?: string, type?: string }} result
*/
( result ) => {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const fetchUrlData = async ( url, options = {} ) => {
const protocol = getProtocol( url );

if (
! protocol ||
! isValidProtocol( protocol ) ||
! protocol.startsWith( 'http' ) ||
! /^https?:\/\/[^\/\s]/i.test( url )
Expand Down
2 changes: 1 addition & 1 deletion packages/core-data/src/hooks/use-entity-record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
import useQuerySelect from './use-query-select';
import { store as coreStore } from '../';
import { Status } from './constants';
import type { Status } from './constants';

interface EntityRecordResolution< RecordType > {
/** The requested entity record */
Expand Down
Loading

0 comments on commit 93d8751

Please sign in to comment.