private void OnCmd_CanvasUnsubscribe(JObject payload) { //Stop if we're already busy if (subscribing) { return; } subscribing = true; //If we have a set canvas, unsubscribe if (canvas != null) { canvas.Unsubscribe(this).GetAwaiter().GetResult(); canvas = null; } subscribing = false; }
/// <summary> /// Gets a canvas from the database or from memory /// </summary> /// <param name="id"></param> /// <returns></returns> public static async Task <LoadedCanvas> GetCanvas(ObjectId id) { LoadedCanvas canvas; lock (Program.canvases) { //Check if we are loaded into memory if (Program.canvases.ContainsKey(id.ToString())) { canvas = Program.canvases[id.ToString()]; } else { //Create canvas object canvas = new LoadedCanvas { canvas_data = new MemoryStream(), data_unsaved = true, subscribers = new List <UserConnection>(), resume_tokens = new Dictionary <string, int>(), canvas_id = id, save_timer = new Timer(60 * 1000) }; //Add Program.canvases.Add(id.ToString(), canvas); //Compute canvas.load_task = canvas.InternalGetCanvas(id); } } //Wait for completion bool loaded = await canvas.load_task; //If loaded, return canvas if (!loaded) { return(null); } else { return(canvas); } }
/// <summary> /// Switches the current active canvas /// </summary> /// <returns></returns> public async Task <bool> ChangeSubscribedCanvas(string id) { //Stop if we're already busy if (subscribing) { return(false); } subscribing = true; //If we have a set canvas, unsubscribe if (canvas != null) { await canvas.Unsubscribe(this); canvas = null; } //Now, get the canvas if (!ObjectId.TryParse(id, out ObjectId canvas_id)) { subscribing = false; return(false); } canvas = await LoadedCanvas.GetCanvas(canvas_id); if (canvas == null) { subscribing = false; return(false); } //Subscribe to this canvas await canvas.Subscribe(this); subscribing = false; return(true); }