Skip to content

Commit 3536399

Browse files
committed
Merge branch 'gh-pages' of https://github.com/LivelyKernel/lively4-core into gh-pages
2 parents 2b07aa0 + 13f92b1 commit 3536399

File tree

7 files changed

+167
-40
lines changed

7 files changed

+167
-40
lines changed

src/client/reactive/active-expression-rewriting/active-expression-rewriting.js

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -216,33 +216,35 @@ class Dependency {
216216

217217
const DependenciesToAExprs = {
218218
_depsToAExprs: new BidirectionalMultiMap(),
219+
_AEsPerFile: new Map(),
219220

220221
associate(dep, aexpr) {
222+
const location = aexpr.meta().get("location").file;
223+
if(!this._AEsPerFile.has(location)) {
224+
this._AEsPerFile.set(location, new Set());
225+
}
226+
this._AEsPerFile.get(location).add(aexpr);
221227
this._depsToAExprs.associate(dep, aexpr);
222228
dep.updateTracking();
223229
debouncedUpdateDebuggingViews();
224230
},
225231

226232
disconnectAllForAExpr(aexpr) {
233+
const location = aexpr.meta().get("location").file;
234+
if(this._AEsPerFile.has(location)) {
235+
this._AEsPerFile.get(location).delete(aexpr);
236+
}
227237
const deps = this.getDepsForAExpr(aexpr);
228238
this._depsToAExprs.removeAllLeftFor(aexpr);
229239
deps.forEach(dep => dep.updateTracking());
230240
debouncedUpdateDebuggingViews();
231241
},
232242

233-
getAETriplesForFile(url) {
234-
const result = [];
235-
for (const ae of this._depsToAExprs.getAllRight()) {
236-
const location = ae.meta().get("location").file;
237-
if (location.includes(url)) {
238-
for (const dependency of this.getDepsForAExpr(ae)) {
239-
for (const hook of HooksToDependencies.getHooksForDep(dependency)) {
240-
result.push({ hook, dependency, ae });
241-
}
242-
}
243-
}
243+
getAEsInFile(url) {
244+
for(const [location, aes] of this._AEsPerFile.entries()) {
245+
if(location.includes(url)) return aes;
244246
}
245-
return result;
247+
return [];
246248
},
247249

248250
getAExprsForDep(dep) {
@@ -275,24 +277,20 @@ const HooksToDependencies = {
275277
this._hooksToDeps.associate(hook, dep);
276278
debouncedUpdateDebuggingViews();
277279
},
280+
278281
remove(hook, dep) {
279282
this._hooksToDeps.remove(hook, dep);
280283
debouncedUpdateDebuggingViews();
281284
},
282285

283-
async getHookTriplesForFile(url) {
284-
const result = [];
285-
for (const hook of this._hooksToDeps.getAllLeft()) {
286-
const locations = await hook.getLocations();
287-
if (locations.some(loc => loc && loc.source.includes(url))) {
288-
for (const dependency of this.getDepsForHook(hook)) {
289-
for (const ae of DependenciesToAExprs.getAExprsForDep(dependency)) {
290-
result.push({ hook, dependency, ae });
291-
}
292-
}
293-
}
294-
}
295-
return result;
286+
async getHooksInFile(url) {
287+
const hooksWithLocations = await Promise.all(this._hooksToDeps.getAllLeft().map(hook => {
288+
return hook.getLocations().then(locations => {return {hook, locations}});
289+
}))
290+
return hooksWithLocations.filter(({hook, locations}) => {
291+
const location = locations.find(loc => loc && loc.source);
292+
return location && locations.source.includes(url);
293+
}).map(({hook, locations}) => hook);
296294
},
297295

298296
disconnectAllForDependency(dep) {
@@ -932,12 +930,19 @@ export async function registerFileForAEDebugging(url, context, triplesCallback)
932930

933931
export async function getDependencyTriplesForFile(url) {
934932
const result = [];
935-
for (const hook of HooksToDependencies._hooksToDeps.getAllLeft()) {
936-
const locations = await hook.getLocations();
933+
for (const ae of DependenciesToAExprs.getAEsInFile(url)) {
934+
for (const dependency of DependenciesToAExprs.getDepsForAExpr(ae)) {
935+
for(const hook of HooksToDependencies.getHooksForDep(dependency)) {
936+
result.push({ hook, dependency, ae });
937+
}
938+
}
939+
}
940+
for (const hook of await HooksToDependencies.getHooksInFile(url)) {
937941
for (const dependency of HooksToDependencies.getDepsForHook(hook)) {
938-
for (const ae of DependenciesToAExprs.getAExprsForDep(dependency)) {
942+
for(const ae of DependenciesToAExprs.getAExprsForDep(dependency)) {
939943
const location = ae.meta().get("location").file;
940-
if (location.includes(url) || locations.some(loc => loc && loc.file.includes(url))) {
944+
// if the AE is also in this file, we already covered it with the previous loop
945+
if (!location.includes(url)){
941946
result.push({ hook, dependency, ae });
942947
}
943948
}

src/client/reactive/active-expression-rewriting/bidirectional-multi-map.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ export default class BidirectionalMultiMap {
2727
this.domainToRange.clear();
2828
this.rangeToDomain.clear();
2929
}
30+
31+
hasRight(val) {
32+
return this.rangeToDomain.has(val);
33+
}
34+
35+
hasLeft(val) {
36+
return this.domainToRange.has(val);
37+
}
3038

3139
getRightsFor(left) {
3240
return this.domainToRange.getOrCreate(left, () => new Set());

src/client/reactive/babel-plugin-polymorphic-identifiers/polymorphic-identifiers.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ export default function ({ types: t, template }) {
3030
return foundDirective;
3131
}
3232

33-
debugger
3433
const shouldTransform = state.opts.executedIn === 'workspace' || hasDirective(path, "pi");
3534
if (!shouldTransform) {
3635
return;

src/client/reactive/polymorphic-identifiers/index.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# Polymorphic-Identifiers
22

33
```JavaScript
4-
import { PIReference } from 'polymorphic-identifiers';
4+
'pi';
5+
import { PIScheme } from 'polymorphic-identifiers';
56

67
// example scheme
7-
class qa extends PIReference {
8+
class qa extends PIScheme {
89
read() {
910
const { elements } = this.parse();
1011
return elements;

src/client/reactive/polymorphic-identifiers/polymorphic-identifiers.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

2-
export class PIReference {
3-
static get isPIReference() { return true; }
2+
export class PIScheme {
3+
static get isPIScheme() { return true; }
44

55
applyOptions(options = {}) {
66
Object.assign(this, options);
@@ -20,7 +20,7 @@ export class PIReference {
2020
}
2121

2222
export function makeRef(referenceClass, options) {
23-
if (referenceClass && referenceClass.isPIReference) {
23+
if (referenceClass && referenceClass.isPIScheme) {
2424
const reference = new referenceClass(options);
2525
reference.applyOptions(options);
2626

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
'pi'
2+
"enable aexpr";
3+
import chai, {expect} from 'src/external/chai.js';
4+
import sinon from 'src/external/sinon-3.2.1.js';
5+
import sinonChai from 'src/external/sinon-chai.js';
6+
chai.use(sinonChai);
7+
8+
import { PIScheme } from 'src/client/reactive/polymorphic-identifiers/polymorphic-identifiers.js';
9+
import { uuid } from 'utils';
10+
11+
describe("PI", function() {
12+
13+
it("is defined", () => {
14+
expect(PIScheme).to.be.ok;
15+
});
16+
17+
it("detects accesses", () => {
18+
class fourtyTwo extends PIScheme {
19+
read() {
20+
return 42;
21+
}
22+
}
23+
24+
expect(fourtyTwo``).to.equal(42)
25+
26+
});
27+
28+
it("can access the `this` reference", () => {
29+
class prop extends PIScheme {
30+
create(strings) {
31+
this.prop = strings.first;
32+
}
33+
read() {
34+
return this.thisReference[this.prop];
35+
}
36+
write(value) {
37+
return this.thisReference[this.prop] = value;
38+
}
39+
}
40+
41+
// read
42+
const o = {
43+
foo: 17,
44+
func() {
45+
return prop`foo`;
46+
},
47+
write(value) {
48+
prop`bar` << value;
49+
}
50+
};
51+
expect(o.func()).to.equal(17)
52+
53+
// write
54+
o.write(23)
55+
expect(o.bar).to.equal(23)
56+
});
57+
58+
it("can access locals with eval", () => {
59+
class local extends PIScheme {
60+
create(strings) {
61+
this.local = strings.first;
62+
}
63+
read() {
64+
return this.evalFunction(this.local);
65+
}
66+
write(value) {
67+
const id = uuid();
68+
self[id] = value;
69+
try {
70+
return this.evalFunction(this.local + ` = self['${id}']`);
71+
} finally {
72+
delete self[id];
73+
}
74+
}
75+
}
76+
77+
let v1 = 'v1', v2 = 42;
78+
79+
// read
80+
expect(local`v1`).to.equal('v1')
81+
82+
// write
83+
local`v2` << "v2";
84+
expect(v2).to.equal('v2');
85+
86+
// read/write
87+
local`v2` << local`v2` + '.2';
88+
expect(v2).to.equal('v2.2');
89+
});
90+
91+
it("ignore normal tagged template strings", () => {
92+
function foo() {
93+
return 'bar';
94+
}
95+
96+
expect(foo``).to.equal('bar')
97+
});
98+
99+
});
100+
describe("AEs", function() {
101+
102+
it("AExprs insert default constructors", () => {
103+
class A {
104+
constructor(prop) {
105+
this.prop = prop;
106+
}
107+
}
108+
class B extends A {}
109+
110+
const b = new B(42);
111+
expect(b.prop).to.equal(42);
112+
});
113+
114+
});

src/client/reactive/test/polymorphic-identifiers/polymorphic-identifiers.spec.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ import sinon from 'src/external/sinon-3.2.1.js';
44
import sinonChai from 'src/external/sinon-chai.js';
55
chai.use(sinonChai);
66

7-
import { PIReference, makeRef } from 'src/client/reactive/polymorphic-identifiers/polymorphic-identifiers.js';
7+
import { PIScheme, makeRef } from 'src/client/reactive/polymorphic-identifiers/polymorphic-identifiers.js';
88
import { uuid } from 'utils';
99

1010
describe("PI", function() {
1111

1212
it("is defined", () => {
13-
expect(PIReference).to.be.ok;
13+
expect(PIScheme).to.be.ok;
1414
expect(makeRef).to.be.ok;
1515
});
1616

1717
it("detects accesses", () => {
18-
class fourtyTwo extends PIReference {
18+
class fourtyTwo extends PIScheme {
1919
read() {
2020
return 42;
2121
}
@@ -29,7 +29,7 @@ describe("PI", function() {
2929
});
3030

3131
it("can access the `this` reference", () => {
32-
class prop extends PIReference {
32+
class prop extends PIScheme {
3333
create(strings) {
3434
this.prop = strings.first;
3535
}
@@ -65,7 +65,7 @@ describe("PI", function() {
6565
});
6666

6767
it("can access locals with eval", () => {
68-
class local extends PIReference {
68+
class local extends PIScheme {
6969
create(strings) {
7070
this.local = strings.first;
7171
}

0 commit comments

Comments
 (0)