Skip to content

Commit

Permalink
Enable prettier in devcontainer and run Prettier for core
Browse files Browse the repository at this point in the history
  • Loading branch information
jviide committed Sep 19, 2022
1 parent 62e8183 commit 28dfdc0
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 60 deletions.
37 changes: 15 additions & 22 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
{
"name": "preact/signals",
"build": {
"dockerfile": "Dockerfile"
},
"customizations": {
"vscode": {
"settings": {
"editor.defaultFormatter": "esbenp.prettier-vscode",
"eslint.format.enable": true,
"explorer.excludeGitIgnore": true,
"[javascript][javascriptreact][typescript][typescriptreact]": {
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
}
},
"extensions":[
"dbaeumer.vscode-eslint",
"esbenp.prettier-vscode"
]
}
},
"postCreateCommand": "pnpm i",
"remoteUser": "node"
"name": "preact/signals",
"build": {
"dockerfile": "Dockerfile"
},
"customizations": {
"vscode": {
"settings": {
"editor.defaultFormatter": "esbenp.prettier-vscode",
"explorer.excludeGitIgnore": true
},
"extensions": ["dbaeumer.vscode-eslint", "esbenp.prettier-vscode"]
}
},
"postCreateCommand": "pnpm i",
"remoteUser": "node"
}
50 changes: 26 additions & 24 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function endBatch() {
effect._nextBatchedEffect = undefined;
effect._flags &= ~NOTIFIED;

if (!(effect._flags & DISPOSED) && (effect._flags & OUTDATED)) {
if (!(effect._flags & DISPOSED) && effect._flags & OUTDATED) {
try {
effect._callback();
} catch (err) {
Expand Down Expand Up @@ -170,7 +170,6 @@ function addDependency(signal: Signal): Node | undefined {
return node;
}
return undefined;

}

declare class Signal<T = any> {
Expand Down Expand Up @@ -217,11 +216,11 @@ function Signal(this: Signal, value?: unknown) {
this._targets = undefined;
}

Signal.prototype._refresh = function() {
Signal.prototype._refresh = function () {
return true;
};

Signal.prototype._subscribe = function(node) {
Signal.prototype._subscribe = function (node) {
if (!(node._flags & NODE_SUBSCRIBED)) {
node._flags |= NODE_SUBSCRIBED;
node._nextTarget = this._targets;
Expand All @@ -233,7 +232,7 @@ Signal.prototype._subscribe = function(node) {
}
};

Signal.prototype._unsubscribe = function(node) {
Signal.prototype._unsubscribe = function (node) {
if (node._flags & NODE_SUBSCRIBED) {
node._flags &= ~NODE_SUBSCRIBED;

Expand All @@ -253,19 +252,19 @@ Signal.prototype._unsubscribe = function(node) {
}
};

Signal.prototype.subscribe = function(fn) {
Signal.prototype.subscribe = function (fn) {
return effect(() => fn(this.value));
};

Signal.prototype.valueOf = function() {
Signal.prototype.valueOf = function () {
return this.value;
};

Signal.prototype.toString = function() {
Signal.prototype.toString = function () {
return this.value + "";
};

Signal.prototype.peek = function() {
Signal.prototype.peek = function () {
return this._value;
};

Expand Down Expand Up @@ -300,7 +299,7 @@ Object.defineProperty(Signal.prototype, "value", {
endBatch();
}
}
}
},
});

function signal<T>(value: T): Signal<T> {
Expand Down Expand Up @@ -439,7 +438,7 @@ function Computed(this: Computed, compute: () => unknown) {

Computed.prototype = new Signal() as Computed;

Computed.prototype._refresh = function() {
Computed.prototype._refresh = function () {
this._flags &= ~NOTIFIED;

if (this._flags & RUNNING) {
Expand All @@ -456,7 +455,7 @@ Computed.prototype._refresh = function() {
if (this._globalVersion === globalVersion) {
return true;
}
this._globalVersion = globalVersion
this._globalVersion = globalVersion;

const prevContext = evalContext;
try {
Expand All @@ -468,7 +467,10 @@ Computed.prototype._refresh = function() {
// is re-evaluated at this point.
let node = this._sources;
while (node !== undefined) {
if (!node._source._refresh() || node._source._version !== node._version) {
if (
!node._source._refresh() ||
node._source._version !== node._version
) {
// If a dependency has something blocking it refreshing (e.g. a dependency
// cycle) or there's a new version of the dependency, then we need to recompute.
break;
Expand Down Expand Up @@ -506,7 +508,7 @@ Computed.prototype._refresh = function() {
return true;
};

Computed.prototype._subscribe = function(node) {
Computed.prototype._subscribe = function (node) {
if (this._targets === undefined) {
this._flags |= OUTDATED | AUTO_SUBSCRIBE;

Expand All @@ -523,7 +525,7 @@ Computed.prototype._subscribe = function(node) {
Signal.prototype._subscribe.call(this, node);
};

Computed.prototype._unsubscribe = function(node) {
Computed.prototype._unsubscribe = function (node) {
Signal.prototype._unsubscribe.call(this, node);

// Computed signal unsubscribes from its dependencies from it loses its last subscriber.
Expand All @@ -540,7 +542,7 @@ Computed.prototype._unsubscribe = function(node) {
}
};

Computed.prototype._notify = function() {
Computed.prototype._notify = function () {
if (!(this._flags & NOTIFIED)) {
this._flags |= OUTDATED | NOTIFIED;

Expand All @@ -554,7 +556,7 @@ Computed.prototype._notify = function() {
}
};

Computed.prototype.peek = function() {
Computed.prototype.peek = function () {
if (!this._refresh()) {
cycleDetected();
}
Expand All @@ -578,7 +580,7 @@ Object.defineProperty(Computed.prototype, "value", {
throw this._value;
}
return this._value;
}
},
});

function computed<T>(compute: () => T): Computed<T> {
Expand Down Expand Up @@ -638,13 +640,13 @@ function Effect(this: Effect, compute: () => void, flags: number) {
this._nextBatchedEffect = undefined;
this._flags = IS_EFFECT | OUTDATED | flags;

if ((flags & AUTO_DISPOSE) && evalContext !== undefined) {
if (flags & AUTO_DISPOSE && evalContext !== undefined) {
this._nextNestedEffect = evalContext._effects;
evalContext._effects = this;
}
}

Effect.prototype._callback = function() {
Effect.prototype._callback = function () {
const finish = this._start();
try {
if (!(this._flags & DISPOSED)) {
Expand All @@ -655,7 +657,7 @@ Effect.prototype._callback = function() {
}
};

Effect.prototype._start = function() {
Effect.prototype._start = function () {
if (this._flags & RUNNING) {
cycleDetected();
}
Expand All @@ -671,15 +673,15 @@ Effect.prototype._start = function() {
return endEffect.bind(this, prevContext);
};

Effect.prototype._notify = function() {
Effect.prototype._notify = function () {
if (!(this._flags & NOTIFIED)) {
this._flags |= NOTIFIED | OUTDATED;
this._nextBatchedEffect = batchedEffect;
batchedEffect = this;
}
};

Effect.prototype._dispose = function() {
Effect.prototype._dispose = function () {
this._flags |= DISPOSED;

if (!(this._flags & RUNNING)) {
Expand All @@ -701,5 +703,5 @@ export {
effect,
batch,
Signal,
type Computed as ReadonlySignal
type Computed as ReadonlySignal,
};
34 changes: 20 additions & 14 deletions packages/core/test/signal.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ describe("effect()", () => {
}
});
expect(spy).not.to.be.called;
expect(() => a.value = 1).to.throw("hello");
expect(() => (a.value = 1)).to.throw("hello");
expect(spy).to.be.calledOnce;
a.value = 0;
expect(spy).to.be.calledOnce;
Expand All @@ -317,7 +317,7 @@ describe("effect()", () => {
if (a.value === 0) {
return () => {
throw new Error("hello");
}
};
} else {
spy();
}
Expand Down Expand Up @@ -412,7 +412,7 @@ describe("effect()", () => {
};
});
});
expect(dispose).to.throw(/error (1|2)/)
expect(dispose).to.throw(/error (1|2)/);
});

it("should throw on cycles", () => {
Expand Down Expand Up @@ -1451,9 +1451,11 @@ describe("batch/transaction", () => {
});

it("should throw the error raised from the callback", () => {
expect(() => batch(() => {
throw Error("hello");
})).to.throw("hello");
expect(() =>
batch(() => {
throw Error("hello");
})
).to.throw("hello");
});

it("should delay writes", () => {
Expand Down Expand Up @@ -1610,11 +1612,13 @@ describe("batch/transaction", () => {
spy1.resetHistory();
spy2.resetHistory();

expect(() => batch(() => {
a.value++;
b.value++;
throw Error("hello");
})).to.throw("hello");
expect(() =>
batch(() => {
a.value++;
b.value++;
throw Error("hello");
})
).to.throw("hello");

expect(spy1).to.be.calledOnce;
expect(spy2).to.be.calledOnce;
Expand Down Expand Up @@ -1644,9 +1648,11 @@ describe("batch/transaction", () => {
spy1.resetHistory();
spy2.resetHistory();

expect(() => batch(() => {
a.value++;
})).to.throw("hello");
expect(() =>
batch(() => {
a.value++;
})
).to.throw("hello");

expect(spy1).to.be.calledOnce;
expect(spy2).to.be.calledOnce;
Expand Down

0 comments on commit 28dfdc0

Please sign in to comment.