Skip to content

Commit 8ac9292

Browse files
committed
workbench: support reloading nodes from workspace.json and register as callback to backend with ChangeNotifier. set callback to window.reloadNodes in browser backend for testing.
1 parent e9adf6c commit 8ac9292

File tree

5 files changed

+38
-16
lines changed

5 files changed

+38
-16
lines changed

lib/backend/browser.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
import {RawNode} from "../model/mod.ts";
2-
2+
import { SearchIndex, FileStore, ChangeNotifier } from "./mod.ts";
33

44
export class BrowserBackend {
55
auth: null;
66
index: SearchIndex;
77
files: FileStore;
8+
changes?: ChangeNotifier;
89

910
constructor() {
1011
this.auth = null;
11-
this.files = new FileStore();
12+
this.files = new LocalStorageFileStore();
1213
if (window.MiniSearch) {
1314
this.index = new SearchIndex_MiniSearch();
1415
} else {
1516
this.index = new SearchIndex_Dumb();
1617
}
18+
this.changes = {
19+
registerNotifier(cb: (nodeIDs: string[]) => void) {
20+
window.reloadNodes = cb;
21+
}
22+
};
1723
}
1824
}
1925

@@ -84,8 +90,8 @@ export class SearchIndex_Dumb {
8490

8591

8692

87-
export class FileStore {
88-
async readFile(path: string): string|null {
93+
export class LocalStorageFileStore {
94+
async readFile(path: string): Promise<string|null> {
8995
return localStorage.getItem(`treehouse:${path}`);
9096
}
9197

lib/backend/github.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ export class GitHubBackend {
262262
}
263263

264264

265-
async readFile(path: string): string|null {
265+
async readFile(path: string): Promise<string|null> {
266266
try {
267267
const resp = await this.client.rest.repos.getContent({
268268
owner: this.user?.userID(),

lib/backend/mod.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,5 @@ export interface FileStore {
4444
}
4545

4646
export interface ChangeNotifier {
47-
onNodeChange(cb: (nodeIDs: string[]) => void);
47+
registerNotifier(cb: (nodeIDs: string[]) => void);
4848
}

lib/workbench/workbench.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export class Workbench {
5454
this.menus = new MenuRegistry();
5555

5656
this.backend = backend;
57-
this.workspace = new Workspace(backend.files);
57+
this.workspace = new Workspace(backend.files, backend.changes);
5858

5959
this.context = {node: null};
6060
this.panels = [];

lib/workbench/workspace.ts

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FileStore } from "../backend/mod.ts";
1+
import { FileStore, ChangeNotifier } from "../backend/mod.ts";
22
import { Bus, Node, RawNode } from "../model/mod.ts";
33
import { Path } from "./mod.ts";
44
import * as module from "../model/module/mod.ts";
@@ -20,12 +20,16 @@ export class Workspace {
2020
expanded: { [key: string]: { [key: string]: boolean } }; // [rootid][id]
2121
settings: {};
2222

23-
constructor(fs: FileStore) {
23+
constructor(fs: FileStore, changes?: ChangeNotifier) {
2424
this.fs = fs;
2525
this.bus = new module.Bus();
2626
this.expanded = {};
2727
this.settings = {};
2828

29+
if (changes) {
30+
changes.registerNotifier(this.reload.bind(this));
31+
}
32+
2933
this.writeDebounce = debounce(async (path, contents) => {
3034
try {
3135
await this.fs.writeFile(path, contents);
@@ -60,16 +64,28 @@ export class Workspace {
6064
}
6165
}
6266

67+
migrateRawNode(n: RawNode): RawNode {
68+
if (n.Name === "treehouse.SearchNode") {
69+
n.Name = "treehouse.SmartNode";
70+
}
71+
return n;
72+
}
73+
74+
async reload(nodeIDs: string[]) {
75+
let doc = JSON.parse(await this.fs.readFile("workspace.json") || "{}");
76+
if (doc.nodes) {
77+
doc.nodes = doc.nodes.filter(n => nodeIDs.includes(n.ID));
78+
doc.nodes = doc.nodes.map(this.migrateRawNode);
79+
this.bus.import(doc.nodes);
80+
m.redraw();
81+
console.log(`Reloaded ${doc.nodes.length} nodes.`);
82+
}
83+
}
84+
6385
async load() {
6486
let doc = JSON.parse(await this.fs.readFile("workspace.json") || "{}");
6587
if (doc.nodes) {
66-
doc.nodes = doc.nodes.map(n => {
67-
// any node migrations:
68-
if (n.Name === "treehouse.SearchNode") {
69-
n.Name = "treehouse.SmartNode";
70-
}
71-
return n;
72-
})
88+
doc.nodes = doc.nodes.map(this.migrateRawNode);
7389
this.bus.import(doc.nodes);
7490
console.log(`Loaded ${doc.nodes.length} nodes.`);
7591
}

0 commit comments

Comments
 (0)