@gedit/resource
Advanced tools
Comparing version 0.1.63 to 0.1.64
@@ -22,3 +22,6 @@ import type { TextDocumentContentChangeEvent } from 'vscode-languageserver-protocol'; | ||
resourceVersion: ResourceVersion | undefined; | ||
protected readonly onDidChangeContentEmitter: Emitter<ResourceChangeEvent<any>>; | ||
protected readonly onPreSaveContentEmitter: Emitter<{ | ||
content: string; | ||
}>; | ||
protected readonly onDidSaveContentEmitter: Emitter<ResourceChangeEvent<any>>; | ||
readonly toDispose: DisposableCollection; | ||
@@ -34,3 +37,6 @@ protected readonly onDirtyChangedEmitter: Emitter<void>; | ||
readonly onDirtyChanged: import("@gedit/utils").Event<void>; | ||
readonly onDidChangeContent: import("@gedit/utils").Event<ResourceChangeEvent<any>>; | ||
readonly onPreSaveContent: import("@gedit/utils").Event<{ | ||
content: string; | ||
}>; | ||
readonly onDidSaveContent: import("@gedit/utils").Event<ResourceChangeEvent<any>>; | ||
readonly onDidChangeValid: import("@gedit/utils").Event<void>; | ||
@@ -58,2 +64,7 @@ readonly onSyncContent: import("@gedit/utils").Event<string | undefined>; | ||
sync(): Promise<void>; | ||
/** | ||
* 用于回退等动作相关数据重载 | ||
* @param content | ||
*/ | ||
reload(content: string): void; | ||
protected doSync(token: CancellationToken): Promise<void>; | ||
@@ -60,0 +71,0 @@ /** |
@@ -86,3 +86,4 @@ "use strict"; | ||
this._dirty = false; | ||
this.onDidChangeContentEmitter = new utils_1.Emitter(); | ||
this.onPreSaveContentEmitter = new utils_1.Emitter(); | ||
this.onDidSaveContentEmitter = new utils_1.Emitter(); | ||
this.toDispose = new utils_1.DisposableCollection(); | ||
@@ -98,3 +99,4 @@ this.onDirtyChangedEmitter = new utils_1.Emitter(); | ||
this.onDirtyChanged = this.onDirtyChangedEmitter.event; | ||
this.onDidChangeContent = this.onDidChangeContentEmitter.event; | ||
this.onPreSaveContent = this.onPreSaveContentEmitter.event; | ||
this.onDidSaveContent = this.onDidSaveContentEmitter.event; | ||
this.onDidChangeValid = this.onDidChangeValidEmitter.event; | ||
@@ -112,3 +114,4 @@ this.onSyncContent = this.onSyncContentEmitter.event; | ||
this.toDispose.push(this.onDidChangeValidEmitter); | ||
this.toDispose.push(this.onDidChangeContentEmitter); | ||
this.toDispose.push(this.onPreSaveContentEmitter); | ||
this.toDispose.push(this.onDidSaveContentEmitter); | ||
this.toDispose.push(this.onErrorEmitter); | ||
@@ -186,2 +189,51 @@ this.toDispose.push(utils_1.Disposable.create(function () { | ||
}; | ||
/** | ||
* 用于回退等动作相关数据重载 | ||
* @param content | ||
*/ | ||
ResourceAutoSaveService.prototype.reload = function (content) { | ||
var _this = this; | ||
// 直接触发数据更新 | ||
this.onSyncContentEmitter.fire(content); | ||
var token = this.cancelSave(); | ||
this.run(function () { return __awaiter(_this, void 0, void 0, function () { | ||
var version, e_1; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
if (token.isCancellationRequested || !this.resource.saveContents || !this._valid) { | ||
return [2 /*return*/]; | ||
} | ||
version = this.resourceVersion; | ||
_a.label = 1; | ||
case 1: | ||
_a.trys.push([1, 3, , 4]); | ||
return [4 /*yield*/, common_1.Resource.save(this.resource, { content: content, options: { encoding: this.options.encoding, version: version } }, token)]; | ||
case 2: | ||
_a.sent(); | ||
// TODO 没有触发DidChange | ||
console.log("-- reload (" + this.resource.uri.displayName + ") --"); | ||
this.contentChanges.length = 0; | ||
this.resourceVersion = this.resource.version; | ||
this.setValid(true); | ||
if (token.isCancellationRequested) { | ||
return [2 /*return*/]; | ||
} | ||
this.setDirty(false); | ||
return [3 /*break*/, 4]; | ||
case 3: | ||
e_1 = _a.sent(); | ||
this.contentChanges.length = 0; | ||
// TODO 可能会有数据丢失 | ||
this.setValid(true); | ||
this.setDirty(false); | ||
if (!common_1.ResourceError.OutOfSync.is(e_1)) { | ||
throw e_1; | ||
} | ||
return [3 /*break*/, 4]; | ||
case 4: return [2 /*return*/]; | ||
} | ||
}); | ||
}); }); | ||
}; | ||
ResourceAutoSaveService.prototype.doSync = function (token) { | ||
@@ -231,3 +283,3 @@ return __awaiter(this, void 0, void 0, function () { | ||
return [2 /*return*/, this.pendingOperation = this.pendingOperation.then(function () { return __awaiter(_this, void 0, void 0, function () { | ||
var e_1; | ||
var e_2; | ||
return __generator(this, function (_a) { | ||
@@ -242,4 +294,4 @@ switch (_a.label) { | ||
case 2: | ||
e_1 = _a.sent(); | ||
console.error(e_1); | ||
e_2 = _a.sent(); | ||
console.error(e_2); | ||
return [3 /*break*/, 3]; | ||
@@ -260,3 +312,3 @@ case 3: return [2 /*return*/]; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var changes, version, lastChanged, content, e_2, isOutOfSync; | ||
var changes, version, lastChanged, content, e_3, isOutOfSync; | ||
return __generator(this, function (_a) { | ||
@@ -277,5 +329,7 @@ switch (_a.label) { | ||
content = this.options.saveAsString(lastChanged); | ||
this.onPreSaveContentEmitter.fire({ content: content }); | ||
return [4 /*yield*/, common_1.Resource.save(this.resource, { content: content, options: { encoding: this.options.encoding, version: version } }, token)]; | ||
case 2: | ||
_a.sent(); | ||
this.onDidSaveContentEmitter.fire(lastChanged); | ||
console.log("-- save (" + this.resource.uri.displayName + ") --"); | ||
@@ -289,6 +343,5 @@ this.contentChanges.length = 0; | ||
this.setDirty(false); | ||
this.onDidChangeContentEmitter.fire(lastChanged); | ||
return [3 /*break*/, 4]; | ||
case 3: | ||
e_2 = _a.sent(); | ||
e_3 = _a.sent(); | ||
this.contentChanges.length = 0; | ||
@@ -298,3 +351,3 @@ // TODO 可能会有数据丢失 | ||
this.setDirty(false); | ||
isOutOfSync = common_1.ResourceError.OutOfSync.is(e_2); | ||
isOutOfSync = common_1.ResourceError.OutOfSync.is(e_3); | ||
// 自动同步 | ||
@@ -304,5 +357,5 @@ if (isOutOfSync) { | ||
} | ||
this.onErrorEmitter.fire(e_2); | ||
this.onErrorEmitter.fire(e_3); | ||
if (!isOutOfSync) { | ||
throw e_2; | ||
throw e_3; | ||
} | ||
@@ -317,3 +370,3 @@ return [3 /*break*/, 4]; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var content, e_3; | ||
var content, e_4; | ||
return __generator(this, function (_a) { | ||
@@ -329,9 +382,9 @@ switch (_a.label) { | ||
case 2: | ||
e_3 = _a.sent(); | ||
e_4 = _a.sent(); | ||
this.setValid(false); | ||
if (common_1.ResourceError.NotFound.is(e_3)) { | ||
if (common_1.ResourceError.NotFound.is(e_4)) { | ||
this.setValid(true); | ||
return [2 /*return*/, undefined]; | ||
} | ||
throw e_3; | ||
throw e_4; | ||
case 3: return [2 /*return*/]; | ||
@@ -338,0 +391,0 @@ } |
{ | ||
"name": "@gedit/resource", | ||
"version": "0.1.63", | ||
"version": "0.1.64", | ||
"license": "MIT", | ||
@@ -31,3 +31,3 @@ "main": "lib/common/index", | ||
}, | ||
"gitHead": "9e9f61f5ab9f1696ef5cf0875966dea9402a2c30" | ||
"gitHead": "d4768cc579938358b825db53ce7c65445a131850" | ||
} |
@@ -32,3 +32,4 @@ import { injectable, inject } from 'inversify'; | ||
resourceVersion: ResourceVersion | undefined; // 服务器资源版本 | ||
protected readonly onDidChangeContentEmitter = new Emitter<ResourceChangeEvent>(); | ||
protected readonly onPreSaveContentEmitter = new Emitter<{ content: string }>(); | ||
protected readonly onDidSaveContentEmitter = new Emitter<ResourceChangeEvent>(); | ||
readonly toDispose = new DisposableCollection(); | ||
@@ -44,3 +45,4 @@ protected readonly onDirtyChangedEmitter = new Emitter<void>(); | ||
readonly onDirtyChanged = this.onDirtyChangedEmitter.event; | ||
readonly onDidChangeContent = this.onDidChangeContentEmitter.event; | ||
readonly onPreSaveContent = this.onPreSaveContentEmitter.event; | ||
readonly onDidSaveContent = this.onDidSaveContentEmitter.event; | ||
readonly onDidChangeValid = this.onDidChangeValidEmitter.event; | ||
@@ -60,3 +62,4 @@ readonly onSyncContent = this.onSyncContentEmitter.event; | ||
this.toDispose.push(this.onDidChangeValidEmitter); | ||
this.toDispose.push(this.onDidChangeContentEmitter); | ||
this.toDispose.push(this.onPreSaveContentEmitter); | ||
this.toDispose.push(this.onDidSaveContentEmitter); | ||
this.toDispose.push(this.onErrorEmitter); | ||
@@ -133,2 +136,38 @@ this.toDispose.push(Disposable.create(() => { | ||
/** | ||
* 用于回退等动作相关数据重载 | ||
* @param content | ||
*/ | ||
reload(content: string): void { | ||
// 直接触发数据更新 | ||
this.onSyncContentEmitter.fire(content); | ||
const token = this.cancelSave(); | ||
this.run(async () => { | ||
if (token.isCancellationRequested || !this.resource.saveContents || !this._valid) { | ||
return; | ||
} | ||
const version = this.resourceVersion as ResourceVersion; | ||
try { | ||
await Resource.save(this.resource, { content, options: {encoding: this.options.encoding, version}}, token); | ||
// TODO 没有触发DidChange | ||
console.log(`-- reload (${this.resource.uri.displayName}) --`); | ||
this.contentChanges.length = 0; | ||
this.resourceVersion = this.resource.version; | ||
this.setValid(true); | ||
if (token.isCancellationRequested) { | ||
return; | ||
} | ||
this.setDirty(false); | ||
} catch (e) { | ||
this.contentChanges.length = 0; | ||
// TODO 可能会有数据丢失 | ||
this.setValid(true); | ||
this.setDirty(false); | ||
if (!ResourceError.OutOfSync.is(e)) { | ||
throw e; | ||
} | ||
} | ||
}); | ||
} | ||
protected async doSync(token: CancellationToken): Promise<void> { | ||
@@ -190,3 +229,5 @@ if (token.isCancellationRequested) { | ||
const content = this.options.saveAsString(lastChanged); | ||
this.onPreSaveContentEmitter.fire({ content }); | ||
await Resource.save(this.resource, { content, options: {encoding: this.options.encoding, version}}, token); | ||
this.onDidSaveContentEmitter.fire(lastChanged); | ||
console.log(`-- save (${this.resource.uri.displayName}) --`); | ||
@@ -200,3 +241,2 @@ this.contentChanges.length = 0; | ||
this.setDirty(false); | ||
this.onDidChangeContentEmitter.fire(lastChanged); | ||
} catch (e) { | ||
@@ -203,0 +243,0 @@ this.contentChanges.length = 0; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
Found 1 instance in 1 package
106855
1783