-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
POC of typed extension views for arbitrary list of extensions.
- Loading branch information
Showing
4 changed files
with
83 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import {s} from '../../json-crdt-patch'; | ||
import {Extension} from '../../json-crdt/extensions/Extension'; | ||
import {ExtNode} from '../../json-crdt/extensions/ExtNode'; | ||
import {ExtApi} from '../../json-crdt/extensions/types'; | ||
import * as ext from '../../json-crdt-extensions/ext'; | ||
import {NodeApi} from '../../json-crdt/model/api/nodes'; | ||
import {Model} from '../../json-crdt/model/Model'; | ||
import {ObjNode} from '../../json-crdt/nodes'; | ||
|
||
export type FileView = {type: 'File'; content: {type: 'Content'}}; | ||
class FileNode extends ExtNode<ObjNode, FileView> { | ||
public readonly extId = 101; | ||
|
||
public name(): string { | ||
return 'File' as const; | ||
} | ||
|
||
public view(): FileView { | ||
return {type: 'File', content: {type: 'Content'}}; | ||
} | ||
} | ||
class FileApi extends NodeApi<FileNode> implements ExtApi<FileNode> {} | ||
export const File = new Extension(101, 'File', FileNode, FileApi, () => | ||
s.obj({type: s.con('File'), content: s.map({})}), | ||
); | ||
|
||
describe('Extensions', () => { | ||
test('type inference', () => { | ||
const schema = s.obj({ | ||
field1: File.new(), | ||
field2: ext.cnt.new(1), | ||
field3: s.con('a'), | ||
field4: ext.mval.new(1), | ||
}); | ||
const model = Model.create(schema, 1, {extensions: [File, ext.cnt, ext.mval]}); | ||
const v = model.view(); | ||
// now typed correctly | ||
type ModelView = typeof v; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters